class Person {
  def static UPLOADED_FILES_DIRECTORY = "./uploaded-files/person"

  static constraints = {
    name(blank:false)
    lastName(blank:false)
    telephone(nullable:true)        
    email(nullable:true)

    cvFilepath(nullable:true)

    birthday(nullable:true)
    personalIdNumber(nullable:true)
    nip(nullable:true)
    accountNumber(nullable:true)

    country(nullable:true)
    city(nullable:true)
    streetNumber(nullable:true)
    postalCode(nullable:true)
  }

  static hasMany = [vacations:Vacation]

  def saveCV(cvFile){
    def directoryPath = Person.UPLOADED_FILES_DIRECTORY + '/' + this.id + '/cv'
    def filePath = directoryPath + '/' + cvFile.getOriginalFilename()
    new File(directoryPath).mkdirs()
    cvFile.transferTo(new File(filePath))
    this.cvFilepath = filePath
    this.save()
  }

  def getCVFile(){
    if (this.cvFilepath)
      return new File(this.cvFilepath)
    return null
  }

  def getCVFileName(){
    if (this.cvFilepath)
      return this.cvFilepath.split('/')[-1]
    return ""
  }

  def getVacationsDates(dateDecorator = { it }){
    def vacationsDates = new HashSet()
    this.vacations.each{
      (it.startDate..it.endDate).each{
	vacationsDates.add(dateDecorator(it))
      }
    }
    return vacationsDates
  }

  def encodeAsHTML(){ "${name} ${lastName}" }
  def toString(){ "${name} ${lastName}" }

  // required data
  String name
  String lastName
  String telephone
  String email

  // personal data is not required
  Date birthday
  String personalIdNumber
  String nip
  String accountNumber
  String cvFilepath

  // address is not required
  String country
  String city
  String streetNumber
  String postalCode
}
